//
//  HTEpLandscapeSelectView.swift
//  MagaCore
//
//  Created by MrMessy on 2023/5/30.
//

import SwiftUI

struct HTEpLandscapeSelectView: View {

  @Binding var var_selEp_id: String?
  @State private var var_animeInfo: HTAnime!
  @State private var var_onHideDialog: (() -> Void)?
  @State private var var_onSelEp: ((HTEp) -> Void)?
  @State private var var_currentSsns: [HTEp] = []
  @State private var var_selEp: HTEp!
  @State private var var_currentSsnTitle: String = ""
  @State private var var_showSelectEpItem = false
  @State private var var_showSelectSsns: Bool = false
  @State private var var_columns: [GridItem] = [GridItem(.fixed(48)),GridItem(.fixed(48)),GridItem(.fixed(48)),GridItem(.fixed(48)),GridItem(.fixed(48))]

  init(var_selEp_id: Binding<String?>, var_animeInfo: HTAnime, var_onHideDialog: (() -> Void)? = nil, var_onSelEp: ((HTEp) -> Void)? = nil) {
    _var_selEp_id = var_selEp_id
    _var_animeInfo = State(initialValue: var_animeInfo)
    _var_onHideDialog = State(initialValue: var_onHideDialog)
    _var_onSelEp = State(initialValue: var_onSelEp)
    _var_selEp = State(initialValue: var_animeInfo.ht_curEp(ep_id: var_selEp_id.wrappedValue ?? ""))
    _var_currentSsns = State(initialValue: var_animeInfo.ht_currentSsns(ep_id: var_selEp_id.wrappedValue ?? ""))
    _var_currentSsnTitle = State(initialValue: var_animeInfo.ht_currentSsnTitle(ep_id: var_selEp_id.wrappedValue ?? ""))
  }

  var body: some View {
    HStack(alignment: .top, spacing: 0){
      Spacer()
      VStack(alignment: .leading, spacing: 0){
        VStack(alignment: .leading) {
          Button() {
            var_showSelectSsns = true
          }label: {
						HStack(spacing: 0) {
							Text(var_currentSsnTitle)
								.ht_commonTextStyle(color: Color(hex: var_orangeColor), fontSize: var_h4, weight: .none)
							Image("expand")
								.frame(width: 12, height: 12)
								.padding(.leading, 2)
						}
						.padding(.leading, 24)
						.padding(.trailing, 24)
						.frame(height: 30, alignment: .center)
						.background(var_orangeBg)
						.cornerRadius(16)
          }

          Spacer()
            .frame(width: 310, height: 1)
            .background(Color(white: 0, opacity: 0.04))
            .padding(.top, 10)
          ZStack(alignment: .topLeading) {
            ScrollView(showsIndicators: false) {
              LazyVGrid(columns: var_columns, alignment: .leading, spacing: 16) {
                ForEach(0..<var_currentSsns.count, id: \.self) { index in
                  let var_item = var_currentSsns[index]
                  let var_selected = var_item.var_id == var_selEp.var_id
                  Button() {
                    if !var_selected {
                      var_onSelEp!(var_item)
                    }
                  } label: {
                    Text(var_item.var_eps_num ?? "")
                      .ht_commonTextStyle(color: Color(hex: var_selected ? var_orangeColor : var_darkColor), fontSize: var_h3, weight: .none)
                      .frame(width: 48, height: 32)
                      .background(var_selected ? var_orangeBg : Color(white: 0, opacity: 0.04))
                      .cornerRadius(4)
                  }
                }
              }
            }
            if var_showSelectSsns {
              VStack(alignment: .leading, spacing: 0) {
                ScrollView(showsIndicators: false) {
                  ForEach(0..<(var_animeInfo.var_ssn_list ?? []).count, id: \.self) { index in
                    let var_str = (var_animeInfo.var_ssn_list ?? [])[index]
                    let var_selected = var_str == var_currentSsnTitle
                    Button() {
                      var_currentSsns = var_animeInfo?.var_eps_dict?[var_str] ?? []
                      var_currentSsnTitle = var_str
                      var_showSelectSsns = false
                    }label: {
                      Text(var_str)
                        .ht_commonTextStyle(color: Color(hex: var_selected ? var_orangeColor : var_darkColor), fontSize: var_h4, weight: .none)
                        .frame(width: 93, height: 36)
                    }
                  }
                }
                .frame(maxHeight: 170, alignment: .leading)
                .background(Color(hex: "ffffff"))
              }
            }
          }
          .padding(.top, 15)
          .padding(.bottom, 20)
        }
        .padding(.leading, var_marginLeft)
        .padding(.trailing, var_marginLeft)
        .padding(.top, 40)
      }
      .frame(maxWidth: 340, maxHeight: .infinity)
      .background(Color(hex: "ffffff"))
      .onTapGesture {}
    }
    .frame(width: UIDevice.ht_screenWidth(), height: UIDevice.ht_screenHeight())
    .background(Color(white:0, opacity: 0.5))
    .onTapGesture {
      var_onHideDialog!()
    }
  }
}
